package ict.kth.se.web;

import ict.kth.se.manager.CookieManager;
import ict.kth.se.manager.PersonManager;
import ict.kth.se.manager.SeekerManager;
import ict.kth.se.web.util.CookieUtils;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	private SeekerManager seekerManager;
	private PersonManager personManager;
	
	@Autowired
	public HomeController(SeekerManager seekerManager, PersonManager personManager) {
		this.seekerManager = seekerManager;
		this.personManager = personManager;
	}
	
	
	/**
	 * Simply selects the home view to render by returning its name.
	 * @param language chosen language
	 * @param request HTTPServletRequest
	 * @param response HTTPServletResponse
	 * @return modelandview
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(@RequestParam(value="language", required=false) String language,
			HttpServletRequest request, HttpServletResponse response) {
		try{
			ModelAndView modelAndView = new ModelAndView("home");
		
			//Handle language
			modelAndView = CookieManager.handleLanguageCookie(modelAndView, request, response, language);
		
			return modelAndView;
		}
		catch(Exception e){
			e.printStackTrace();
			//Create modelAndView to be sent to view for errors
			ModelAndView modelAndView = new ModelAndView("error");
			modelAndView.addObject("error", e.getMessage());
			return modelAndView;
		}
	}
	
}
